package org.orange.aicodeheloper.ai.mcp;

import dev.langchain4j.mcp.McpToolProvider;
import dev.langchain4j.mcp.client.DefaultMcpClient;
import dev.langchain4j.mcp.client.McpClient;
import dev.langchain4j.mcp.client.transport.McpTransport;
import dev.langchain4j.mcp.client.transport.http.StreamableHttpMcpTransport;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class McpConfig {

    @Value("${tavily.api-key}")
    private String tavilyApiKey;

    @Value("${langchain4j.community.dashscope.chat-model.api-key}")
    private String dashScopeMcpApiKey;

    @Bean
    public McpToolProvider mcpToolProvider() {
        // 和 MCP 服务通讯
        McpTransport transport = new StreamableHttpMcpTransport.Builder()
                .url("https://mcp.tavily.com/mcp/?tavilyApiKey=" + tavilyApiKey)
//                .url("https://dashscope.aliyuncs.com/api/v1/mcps/WebSearch/sse")
//                .customHeaders(Map.of("Authorization", "Bearer " + dashScopeMcpApiKey))
                .logRequests(true) // 开启日志，查看更多信息
                .logResponses(true)
                .build();
        // 创建 MCP 客户端
        McpClient mcpClient = new DefaultMcpClient.Builder()
                .transport(transport)
                .build();
        // 从 MCP 客户端获取工具
        return McpToolProvider.builder()
                .mcpClients(mcpClient)
                .build();
    }
}